package com.fz.us.admin.dict.service.impl;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.Result;
import com.fz.us.admin.base.dao.BaseDao;
import com.fz.us.admin.base.service.impl.BaseServiceImpl;
import com.fz.us.admin.dict.dao.DictDao;
import com.fz.us.admin.dict.entity.Dict;
import com.fz.us.admin.dict.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service实现类 - 字典
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-19 。
 * ============================================================================
 */
@Service
public class DictServiceImpl extends BaseServiceImpl<Dict, String> implements DictService{

    @Resource
    private DictDao dictDao;

    @Override
    public BaseDao<Dict, String> getBaseDao() {
        return dictDao;
    }

    @Override
    public List<Dict> listSystem() {
        return dictDao.listSystem();
    }

    @Override
    public List<Dict> listDefault(String keyId) {
        return dictDao.listDefault(keyId);
    }

    @Override
    public Dict getByValue(String value) {
        return get("value", value);
    }

    @Override
    public boolean isExistValue(String value) {
        return getByValue(value) != null ;
    }

    @Override
    public Result addDict(String name, String value, String description, int ifKey) {
        Assert.hasText(name, "name is required");
        Assert.hasText(value, "value is required");

        if (isExistValue(value)) {
            return resultService.fail("关键字已存在");
        }

        Dict dict = new Dict(name, value, description, ifKey);
        return saveEntity(dict);
    }

    @Override
    public Result addDict(String keyId, String name, String value, String description) {
        Assert.hasText(name, "keyId is required");
        Assert.hasText(name, "name is required");
        Assert.hasText(value, "value is required");

        if (isExistValue(value)) {
            return resultService.fail("关键字已存在");
        }

        Dict dict = new Dict(keyId, name, value, description);
        return saveEntity(dict);
    }

    @Override
    public Result editDict(String dictId, String name, String value, String description, int ifKey) {
        Assert.hasText(dictId, "dictId is required");
        Assert.hasText(name, "name is required");
        Assert.hasText(value, "value is required");

        Dict dictTemp = get(dictId);
        if (!StringUtils.equals(dictTemp.getValue(), value)) {
            if (isExistValue(value)) {
                return resultService.fail("关键字已存在");
            }
        }

        dictTemp.setName(name);
        dictTemp.setValue(value);
        dictTemp.setDescription(description);
        dictTemp.setIfKey(ifKey);

        update(dictTemp);

        return resultService.successWithId(dictTemp.getId());
    }

    @Override
    public Map<String, Object> getListMap(Dict dict) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("id", StringUtils.isEmpty(dict.getId())?"":dict.getId());
        map.put("ifSystem", dict.getIfSystem());
        map.put("keyId", StringUtils.isEmpty(dict.getKeyId())?"":dict.getKeyId());
        map.put("name", StringUtils.isEmpty(dict.getName())?"":dict.getName());
        map.put("pinYinHead", StringUtils.isEmpty(dict.getPinYinHead())?"":dict.getPinYinHead().toLowerCase());
        map.put("pinYin", StringUtils.isEmpty(dict.getPinYin())?"":dict.getPinYin());

        map.put("value", StringUtils.isEmpty(dict.getValue())?"":dict.getValue());
        map.put("description", StringUtils.isEmpty(dict.getDescription())?"":dict.getDescription());
        map.put("ifKey", dict.getIfKey());

        // other
        map.put("state", dict.getState()==null?"":dict.getState().name());

        return map;
    }

    @Override
    public Map<String, Object> getDetailMap(Dict dict) {
        return getListMap(dict);
    }

}
